package com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.provider

import android.content.Context
import android.view.View
import com.example.library_common.adapter.multi.JssMultiItemEntity
import com.example.library_common.adapter.multi.JssMultiItemViewHolder
import com.example.library_common.adapter.multi.JssNewMultipleAdapter
import com.example.library_common.album.view.grid_view.XGridLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleEntity
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleFunEntity
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleFunWrapperEntity

class NewSaleFunProvider(view: View?) : JssMultiItemViewHolder<NewSaleFunWrapperEntity>(view) {

    private var mAdapter: CommAdapter<NewSaleFunEntity>? = null

    override fun getItemLayout(): Int {
        return R.layout.new_sale_fun_provider
    }

    override fun convert(context: Context?, m: NewSaleFunWrapperEntity?) {
        super.convert(context, m)
        val mXGridLayout: XGridLayout = findView22(R.id.mXGridLayout)
        if (mAdapter == null) {
            mAdapter =
                object : CommAdapter<NewSaleFunEntity>(
                    context,
                    R.layout.new_sale_fun_item_provider_layout
                ) {
                    override fun convert(
                        holder: XQuickViewHolder,
                        item: NewSaleFunEntity,
                        position: Int
                    ) {
                        holder.setImageResource(R.id.icon, item.icon)
                            .setText(R.id.title_template, item.title)
                            .setOnClickListener(
                                R.id.funView
                            ) {
                                if (adapter is JssNewMultipleAdapter<*>) {
                                    val itemViewClick =
                                        (adapter as JssNewMultipleAdapter<JssMultiItemEntity>)
                                            .adapterViewItemViewClick
                                    itemViewClick?.onAdapterViewListItemViewClick(
                                        item as JssMultiItemEntity,
                                        NewSaleEntity.NEW_SALE_FUN_TYPE
                                    )
                                }
                            }
                    }
                }
            if (adapter is JssNewMultipleAdapter<*>) {
                mXGridLayout.maxItem = m?.getData()?.size ?: 0
                mXGridLayout.setAdapter(
                    mAdapter,
                    (adapter as JssNewMultipleAdapter<*>).lifecycle
                )
            }
        }

        mAdapter?.addAll(m?.getData() as MutableList<NewSaleFunEntity>)
    }

}